import {reqLogin,reqLogout,reqGetRegisterCode,reqRegister,reqGetUserInfoByToken} from '@/api'
import {localStorageToken} from '@/utils/token'
const state = {
    userInfo:{},
    // aToken:''
}
const actions = {
    // 登录
    async login({commit},params){
        const result = await reqLogin(params)
        if(result.code===200){
            // 将token持久性存储
            localStorageToken(result.data.token)
            // commit('A',result.data.token)
            return 'ok'
        }
        else return Promise.reject(new Error('fail'))
    },
    // 退出登录
    async logout({commit}){
        const result = await reqLogout() 
        if(result.code===200){
            commit("LOGOUT")
            return 'ok'
        }
        else return Promise.reject(new Error('fail'))
    },
    // 通过token获取用户信息展示
    async getUserInfoByToken({commit}){
        const result = await reqGetUserInfoByToken()
        if(result.code===200){
            commit('GETUSERINFOBYTOKEN',result.data)
            return 'ok'
        }else return Promise.reject(new Error('fail'))
    },
    // 注册
    // 获取验证码
    async getRegisterCode({commit},phone){
        const result = await reqGetRegisterCode(phone)
        if(result.code===200) return result.data
        else return Promise.reject(new Error('fail'))
    },
    // 注册按钮
    async register({commit},params){
        const result = await reqRegister(params)
        if(result.code===200) return 'ok'
        else return Promise.reject(new Error('fail'))
    }
}
const mutations = {
    // 存储用户信息
    GETUSERINFOBYTOKEN(state,data){
        state.userInfo = data
    },
    // 退出登录
    LOGOUT(state){
        // 清除本地存储中的token
        localStorage.removeItem('token')
        // 清除vuex中存储的用户信息 userInfo
        state.userInfo = {}
    },
    // A(state,data){
    //     state.aToken = data
    // }
}
const getters = {}

export default {state,actions,mutations,getters}